package com.zhiyou100.controller;

import com.zhiyou100.entity.Exam;
import com.zhiyou100.entity.ResponseEntity;
import com.zhiyou100.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
@Controller
public class RestFulAction {
        @Autowired
        private ExamService examService;
    //获取一个
    @RequestMapping(value ="/restful_exams/{id}",method ={RequestMethod.GET})
    public  @ResponseBody ResponseEntity<Exam>  getOne(@PathVariable("id") int eid){
        System.out.println("getOne");
        Exam e=examService.getOne(eid);
        return new ResponseEntity<Exam>(200,null,e);
    }


    //获取所有
    @RequestMapping(value ="/restful_exams",method ={RequestMethod.GET})
    @ResponseBody
    public   ResponseEntity<List<Exam>>  getAll(){
        System.out.println("getAll");
        List<Exam> list=examService.getAll();
        return new ResponseEntity<List<Exam>>(200,null,list);
    }
    //分页查询
    @RequestMapping(value ="/restful_exams/{startIndex}/{pageSize}",method ={RequestMethod.GET})
    @ResponseBody
    public   ResponseEntity<List<Exam>>  getAllFenYe(@PathVariable("startIndex")Integer startIndex, @PathVariable("pageSize")Integer pageSize)throws Exception{
        System.out.println("getAllFenYe");
        List<Exam> list=examService.getAllFenYe(startIndex,pageSize);
        return new ResponseEntity<List<Exam>>(200,null,list);
    }
     //添加一个：接受json对象
        @PostMapping(value ="/restful_exams1")
        @ResponseBody
        public  ResponseEntity<Exam> addOne1(@RequestBody Exam  exam){
            System.out.println("addOne1");
            examService.addOne(exam);
            return new ResponseEntity<Exam>(200,"添加成功",null);
        }
        //添加一个：接受formdata的数据  用普通参数即可
        @PostMapping(value ="/restful_exams2")
        @ResponseBody
        public   ResponseEntity<Exam> addOne2(Exam  exam){
            System.out.println("addOne2");
            examService.addOne(exam);
            return new ResponseEntity<Exam>(200,"添加成功",null);
        }
        //删除一个
    @RequestMapping(value ="/restful_exams/{id}",method ={RequestMethod.DELETE})
    public  @ResponseBody ResponseEntity<String>  deleteOne(@PathVariable("id") Integer eid){
        System.out.println("deleteOne");
        examService.deleteOne(eid);
        return new ResponseEntity<String>(200,"删除成功！",null);
    }
        //修改一个
        //@RequestMapping(value ="/exams",method ={RequestMethod.PUT})
        @PutMapping("/restful_exams")
        @ResponseBody
        public   ResponseEntity<String>  updateOne(@RequestBody Exam exam)throws Exception{
            System.out.println("updateOne");
            examService.updateOne(exam);
            return new ResponseEntity<String>(200,"修改成功！",null);
        }

        //文件上传  接受formdata的数据  用普通参数接受普通组件  用
        @PostMapping(value ="/restful_exams3")
        @ResponseBody
        public   ResponseEntity<Exam> addOne3(@RequestParam("examText") MultipartFile examText, Exam  exam, HttpServletRequest req)throws Exception{

            String path=req.getSession().getServletContext().getRealPath("/imgs");
            //获取文件名字
            String fileName=examText.getOriginalFilename();
            String fileHZ=fileName.substring(fileName.lastIndexOf("."));
            fileName=exam.getEname()+fileHZ;//保存的文件名字和考试名字一致
            //获取文件的后缀
            File  muDiFile=new File(path,fileName);
            examText.transferTo(muDiFile);//实现文件的读写
            exam.setEurl(fileName);
            examService.addOne(exam);
            return new ResponseEntity<Exam>(200,"添加成功",null);
        }
}
